package com.thti.mecool.client.widget;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Checkable;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.thti.mecool.client.activity.R;

public class MyCheckText extends LinearLayout implements Checkable {
	private ImageView iv;
	private TextView tv;
	private OnCheckedListener onCheckedListener;
	private boolean checked;

	public MyCheckText(Context context) {
		super(context);
		init(context);
	}

	public MyCheckText(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	private void init(Context context) {
		View v = LayoutInflater.from(context)
				.inflate(R.layout.check_text, null);
		iv = (ImageView) v.findViewById(R.id.check_text_iv);
		tv = (TextView) v.findViewById(R.id.check_text_tv);
		iv.setImageResource(R.drawable.mecool_checkbox);
		setChecked(false);
		addView(v);
		setOnClickListener(onClickListener);
	}

	public void setText(CharSequence arg0) {
		tv.setText(arg0);
	}

	@Override
	public boolean isChecked() {
		return checked; 
	}

	@Override
	public void setChecked(boolean checked) {
		this.checked = checked;
		if (onCheckedListener != null)
			onCheckedListener.isChecked(checked);
	}

	@Override
	public void toggle() {
		setChecked(!isChecked());
	}

	public void setOnCheckedListener(OnCheckedListener onCheckedListener) {
		this.onCheckedListener = onCheckedListener;
	}

	private OnClickListener onClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			if (isChecked()) {
				iv.setImageResource(R.drawable.mecool_checkbox);
				toggle();
			} else {
				iv.setImageResource(R.drawable.mecool_checkbox_click);
				toggle();
			}
		}
	};
	public interface OnCheckedListener{
		public void isChecked(boolean isChecked);
	}
}
